package com.koron.order.risk.base.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.risk.base.bean.po.LineRiskChange;
import com.koron.order.risk.base.bean.vo.LineRiskChangeVO;
import com.koron.order.risk.base.service.LineRiskChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "风险管控单变更接口")
@RestController
@RequestMapping("lineRiskChange")
public class LineRiskChangeController implements WorkflowController {

    @Getter
    @AllArgsConstructor
    public enum RiskChangeEnum{
        DRAFT(0,"草稿"),
        WAIT_REVIEW(1,"待审核"),
        ARCHIVED(2,"已归档");


        private Integer status;
        private String desc;
    }

    @Autowired
    private LineRiskChangeService lineRiskChangeService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 根据风险管控单id找到对应的风险管控措施
     * @param id
     * @return
     */
    @ApiOperation(value = "根据风险管控变更单id找到对应的风险管控措施")
    @GetMapping("getById")
    public Response getRiskMeasureById(String id){
        Assert.notNull(id,"id不能为空！");
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskChangeService, "getRiskChangeById", LineRiskChangeVO.class, id));
    }

    @ApiOperation(value = "分页查询风险管控变更单")
    @GetMapping("page")
    public Response listRiskChangePage(PageQuery pageQuery, LineRiskChange lineRiskChange){
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskChangeService, "page", PageInfo.class, pageQuery, lineRiskChange));
    }

    @ApiOperation(value = "新增或更新风险管控变更单")
    @PostMapping("saveOrUpdate")
    public Response saveOrUpdate(@RequestBody LineRiskChange lineRiskChange){
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskChangeService, "saveOrUpdate", LineRiskChange.class, lineRiskChange));
    }

    @ApiOperation(value = "删除风险管控变更单")
    @GetMapping("delete")
    public Response delete(String id){
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskChangeService, "delete", Boolean.class, id));
    }

    @ApiOperation(value = "更新风险管控变更单状态")
    @GetMapping("updateStatus")
    public Response updateStatus(String id, Integer status){
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskChangeService, "updateStatus", Boolean.class, id, status));
    }


    @ApiOperation(value = "更新原来的风险管控单中的变更措施的时间")
    @GetMapping("updateMeasure")
    public Response updateMeasure(String id, Integer status){
        LineRiskChange lineRiskChange = new LineRiskChange();
        lineRiskChange.setId(id);
        lineRiskChange.setStatus(status);
        return   Response.ok(ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), lineRiskChangeService, "updateMeasure", Boolean.class, lineRiskChange));

    }

    @Override
    public Response<Object> updateState(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 撤回或者废弃
        String id = redirectQuery.getDataId();
        Integer status = null;
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 修改为草稿状态
           status = RiskChangeEnum.DRAFT.getStatus();
        } else {
            status = RiskChangeEnum.WAIT_REVIEW.getStatus();
        }

        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), lineRiskChangeService, "updateStatus", Boolean.class, id, status);
        return Response.success();
    }

    @Override
    public Response<Object> updateEnd(RedirectQuery redirectQuery) {
        String id = redirectQuery.getDataId();
        Integer status = RiskChangeEnum.ARCHIVED.getStatus();
        LineRiskChange lineRiskChange = new LineRiskChange();
        lineRiskChange.setId(id);
        lineRiskChange.setStatus(status);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), lineRiskChangeService, "updateMeasure", Boolean.class, lineRiskChange);
        return Response.success();
    }
}
